<?php
class index_controller extends controller{

	public $current = "index";
	public $in = true;
	public function __construct(){
		parent::__construct();
		spider::write($GLOBALS['W']['id']);
	}

	public function index(){
		$yuming_id = $this->yuming_website['id'];
		$this->current = M;
		$title = "";
		$typeid = 'all';
		$typename = "首页";
		$url = $this->rest_uri;
		if($url == 'sitemap.xml'){
			$this->sitemap();
		}else{
			$addSql = "";
			$orderBy = " order by id desc";
                        $second_t = null;
                        if(!empty(A)) {
                            $second_t = M.'/'.A;
                        }
			if(isset($this->sitenav[M])){
				$id = $typeid = $ename = M;
				$addSql .=  " and class = '$ename' ";
				$title = $typename = $this->sitenav[M]['title'];
                        }elseif(isset($this->sitenav[$second_t])){
                                $id = $typeid = $ename = $second_t;
				$addSql .=  " and class = '$ename' ";
				$title = $typename = $this->sitenav[$second_t]['title'];
                        }elseif(M!='index'){
				self::page_404();
			}
                        
			$showtypes = array('new'=>'最新', 'hot'=>'热点', 'photo'=>'图文');
			if(empty($url)||preg_match('#/news#', $url)){
				$showtype = 'new';
			}elseif(preg_match('#/hot#', $url)){
				$showtype = 'hot';
				$orderBy = " order by click desc,id desc";
			}elseif(preg_match('#/photo#', $url)){
				$showtype = 'photo';
				$addSql .=  " and thumb<>'' ";
			} else {
				$showtype = 'new';
			}

			$page = 1;
			if(preg_match("#.*?/list_(\d*).html#si", $url,$mt)){
				$page = $mt[1];
			}

			$pagesize = gp('pagesize') ? gp('pagesize') : 20;
			if(IS_AJAX) {
				$offset = ($page - 1) * $pagesize;
				$article_list = db::select("select * from `content_{$yuming_id}` where status = 1 {$addSql} {$orderBy} limit {$offset},{$pagesize}");
				$this->json($article_list);
			} else {
				
				$keywords = $this->yuming_website['keywords'];
				$description = $this->yuming_website['description'];
				$position = "<a href='/'>{$this->yuming_website['sitename']}</a> > <a href='/{$typeid}'>{$typename}</a>";
				if(isset($this->sitenav[M]) || isset($this->sitenav[$second_t])){
					$pagetitle = ($title? $title.'-' : '') . $this->yuming_website['sitename'];
					require $this->tpl('list_article');
				} else {
					$pagetitle = ($title? $title.'-' : '') . ($this->yuming_website['siteTitle'] ? $this->yuming_website['siteTitle'] : $this->yuming_website['sitename']);
					require $this->tpl('index');
				}
			}
		}	
	}

	public function sitemap(){
		if($GLOBALS['G']['sitemap'] == 1){
			header("Content-Type:text/xml");
			$yuming_id = $this->yuming_website['id'];
			$topic_list = db::select("select * from `content_{$yuming_id}` where status = 1 order by id desc limit 1000");
			$str  = '<urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9">';
			$str_list  = "";
			foreach ($topic_list as $key  => $value) {
				$str_list .= "<url>
				<loc>http://".$this->yuming_website['name'].'/'.$value['class'].'/'.$value['id'].".html</loc>
				<lastmod>".date('Y-m-d',$value['addtime'])."</lastmod>
				<changefreq>daily</changefreq>
				<priority>0.9</priority>\n</url>";
			}
			echo $str.$str_list."</urlset>";
		}else{
			$this->page_404();
		}
	}
	
	public function _empty(){
		$url = $this->rest_uri;
		if(preg_match("#\w+/\d*\.html#",$url)){
			$this->view();
		} else {
			$this->index();
		}
	}

	//内容页
	public function view(){
		$showtypes = array('new'=>'最新', 'hot'=>'热点', 'photo'=>'图文');
		$showtype = 'new';
		$yuming_id = $this->yuming_website['id'];
		$x = '';
		$url = $this->rest_uri;
		preg_match("#\w+/(\d+)\.html#",$url,$mt);
		if($mt){
			$id=$mt[1];
			$article_content=db::find("select *,addtime as pubdate from `content_{$yuming_id}` where status=1 and id='$id' ");
			if(empty($article_content)){
				$this->page_404();
			} else {
				$field = $article_content;
				extract($article_content);
			}
			$current = $typeid = $article_content['class'];
			$this->current = $typeid;
			$article_content['typename'] = $this->sitenav[$typeid]['title'];
			$article_content['description'] = strip_tags($article_content['description']);
			$article_content['title'] = strip_tags($article_content['title']);
			
			db::query("update `content_{$yuming_id}` set click = click +1 where id = '$id'");
			
			$text = ymcms::article_read($this->yuming_website['id'], $article_content['id']);
			$seo_keyword = !empty($article_content['seo_keyword'])?'_'.$article_content['seo_keyword']:'';

			$title = $article_content['title'];
			$source = $article_content['source'];
			$writer = "admin";
			$typename = $article_content['typename'];
			
			$typeurl = '/'.$typeid;
			$typelink = '<a href="'.$typeurl.'">'.$typename.'</a>';
			
			$pagetitle = $article_content['title'].$seo_keyword."-".$typename.'-'.$this->yuming_website['sitename'];
			$keywords = empty($article_content['keyword']) ? $this->yuming_website['keywords'] : $article_content['keyword'];
			$description = empty($article_content['description']) ? $this->yuming_website['description'] : $article_content['description'];
			$position = "<a href='/'>{$this->yuming_website['sitename']}</a> > <a href='/{$typeid}'>{$typename}</a> > <span>{$title}</span>";
			$article_content['content'] = $article_content['body'] = $body = $text;
			require $this->tpl('article_article');
		} else {
			$this->page_404();
		}
	}
        

}
